package lotus.blog.lotusblog.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lotus.blog.lotusblog.mapper.ClassificationMapper;
import lotus.blog.lotusblog.mapper.UserMapper;
import lotus.blog.lotusblog.pojo.Classification;
import lotus.blog.lotusblog.pojo.User;
import lotus.blog.lotusblog.utils.JwtUtil;
import lotus.blog.lotusblog.utils.ResultTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/classification")
public class ClassificationController {
    @Autowired
    UserMapper userMapper;
    @Autowired
    ClassificationMapper classificationMapper;
    @Autowired
    JwtUtil jwtUtil;
    @GetMapping("/get")
    public ResultTemplate get(@RequestParam("username") String username){
        QueryWrapper<User> userWrapper = new QueryWrapper<User>();
        userWrapper.eq("username",username);
        User user = userMapper.selectOne(userWrapper);
        if (user == null){
            return ResultTemplate.fail("用户不存在");
        }
        QueryWrapper<Classification> wrapper = new QueryWrapper<Classification>();
        wrapper.eq("user_id",user.getId());
        List<Classification> classifications = classificationMapper.selectList(wrapper);
        return ResultTemplate.succ("获取成功",classifications);
    }
    @PostMapping("/add")
    public ResultTemplate add(@RequestBody Classification classification, HttpServletRequest request){
        User user = jwtUtil.getUserBytoken(request);
        if (user.getId()!=classification.getUserId()){
            return ResultTemplate.authFail("非法信息");
        }
        try{
            int insert = classificationMapper.insert(classification);
        }catch (Exception e){
            e.printStackTrace();
            return ResultTemplate.fail("添加失败");
        }
        return ResultTemplate.succ("添加成功",null);
    }
    @PostMapping("/update")
    public ResultTemplate update(@RequestBody Classification classification,HttpServletRequest request) {
        User user = jwtUtil.getUserBytoken(request);
        if (user.getId()!=classification.getUserId()){
            return ResultTemplate.authFail("非法信息");
        }
        try{
            int i = classificationMapper.updateById(classification);
        }catch(Exception e){
            e.printStackTrace();
            return ResultTemplate.fail("修改失败");
        }
        return ResultTemplate.succ("修改成功",null);
    }
    @PostMapping("/delete")
    public ResultTemplate delete(@RequestBody Classification classification,HttpServletRequest request){
        User user = jwtUtil.getUserBytoken(request);
        if (user.getId()!=classification.getUserId()){
            return ResultTemplate.authFail("非法信息");
        }
        try{
            classificationMapper.deleteById(classification);
        }catch(Exception e){
            e.printStackTrace();
            return ResultTemplate.fail("删除失败");
        }
        return ResultTemplate.succ("删除成功",null);
    }
}
